package com.psct.music;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import android.app.Service;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.provider.MediaStore;
import android.util.Log;

import com.psct.Constant;
import com.psct.R;

/**
 * 背景音乐播放类
 * 
 * @author zjingchuan
 * 
 */
public class BgMusicPlayer {
	
	private Context context;
	private MediaPlayer mPlayer;
	private MusicService musicService;
	private boolean isBinded=false;
	private ServiceConnection conn = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			Log.v("music", "音乐服务绑定成功");
			musicService=((MusicService.LocalBinder)service).getService();
			musicService.startStreaming("http://192.168.20.213:8080/recallchina/music/2b69f025-6826-416b-9057-007d875a66ca.mp3");
			isBinded=true;
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			Log.v("music", "音乐服务绑定失败");
		}};
	public BgMusicPlayer(Context context) {
		mPlayer=new MediaPlayer();
		mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		this.context=context;
	}
	public void play(int mode) {
		// TODO Auto-generated method stub
		switch(mode) {
		case Constant.MODEL_NOMAL:
			mPlayer=MediaPlayer.create(context, R.raw.bg_music);
			mPlayer.setLooping(true);
			mPlayer.start();
			break;
		case Constant.MODEL_SDCARD:
			switchToSDcard();
			break;
			
		case Constant.MODEL_NET:
			context.bindService(new Intent(context,MusicService.class), conn, Context.BIND_AUTO_CREATE);
			if(musicService!=null)
			
			break;
		}
	}
	
	public void switchToSDcard(){
		ContentResolver resolver = context.getContentResolver();
		Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] {MediaStore.Audio.Media.DATA}, null, null, null);
		if(cursor==null)
			return;
		final int count = cursor.getCount();
		Log.v("mp3", "count:"+count);
		final Map<Integer,String> musicMap = new HashMap<Integer, String>();
		int i=0;
		while (cursor.moveToNext()) {
			musicMap.put(i,cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
			i++;
			
		}
		cursor.close();
		if(mPlayer==null)
			mPlayer=new MediaPlayer();
		try {
			mPlayer.reset();
			
			mPlayer.setDataSource(musicMap.get((int)(Math.random()*count)));
			//mPlayer.prepare();
			mPlayer.setOnCompletionListener(new OnCompletionListener(){

				@Override
				public void onCompletion(MediaPlayer mp) {
					mPlayer.reset();
					try {
						mPlayer.setDataSource(musicMap.get((int)(Math.random()*count)));
						mPlayer.prepare();
						mPlayer.start();
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (SecurityException e) {
						e.printStackTrace();
					} catch (IllegalStateException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				
			});
			mPlayer.start();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void stop() {
		if(mPlayer!=null&&mPlayer.isPlaying()) {
			mPlayer.stop();
			mPlayer.release();
			mPlayer=null;
		}
		if(isBinded) {
			isBinded=false;
			context.unbindService(conn);
		}
	}
	public boolean isPlay() {
		if(mPlayer==null)
			return false;
		return mPlayer.isPlaying();
	}
}
